package com.example.mq.server.dao;

import com.example.mq.MqApplication;
import com.example.mq.server.core.Binding;
import com.example.mq.server.core.Exchange;
import com.example.mq.server.core.ExchangeType;
import com.example.mq.server.core.MSGQueue;
import com.example.mq.server.mapper.MetaMapper;

import java.io.File;
import java.util.List;

/**
 * 整合数据库的类
 */
public class DataBaseManager {
    private MetaMapper metaMapper;
    // 对数据库进行初始化
    public void init() {
        // 获取metaMapper
        metaMapper = MqApplication.context.getBean(MetaMapper.class);
        // 检查数据库是否存在
        if (!checkDBExists()) {
            // 如果数据库不存在，则创建数据库
            // 创建data目录
            createDir();
            // 创建数据库，数据表
            createDB();
            // 创建默认数据，默认的交换机
            createDefaultData();
            System.out.println("[DataBaseManager] 数据库初始化完成！");
        } else {
            // 存在则不重复创建
            System.out.println("[DataBaseManager] 数据库已经存在！");
        }
    }

    private void createDir() {
        File Dir = new File("./data");
        boolean mkDir = Dir.mkdir();
        if (mkDir) {
            System.out.println("[DataBaseManager] 目录创建成功！");
        } else {
            System.out.println("[DataBaseManager] 目录创建失败！");
        }
    }

    public void deleteDB() {
        File file = new File("./data/meta.db");
        boolean result = file.delete();
        if (result) {
            System.out.println("[DataBaseManager] 数据库删除成功！");
        } else {
            System.out.println("[DataBaseManager] 数据库删除失败！");
        }
        // 删除目录时必须是空的，所以先删除文件
        File dataDir = new File("./data");
        result = dataDir.delete();
        if (result) {
            System.out.println("[DataBaseManager] 目录删除成功！");
        } else {
            System.out.println("[DataBaseManager] 目录删除失败！");
        }
    }

    private void createDefaultData() {
        // 创建一个默认的交换机类型是 direct
        Exchange exchange = new Exchange();
        exchange.setName("");
        exchange.setType(ExchangeType.DIRECT);
        exchange.setDurable(true);
        exchange.setAutoDelete(false);
        metaMapper.insertExchange(exchange);
        System.out.println("[DataBaseManager] 创建初始化数据完成！");
    }

    private void createDB() {
        // 创建数据表，调用相关方法
        metaMapper.createExchangeTable();
        metaMapper.createQueueTable();
        metaMapper.createBindingTable();
        System.out.println("[DataBaseManager] 创建数据表完成！");
    }

    private boolean checkDBExists() {
        // 检查数据库是否存在
        File file = new File("./data/meta.db");
        return file.exists();
    }

    public void insertExchange(Exchange exchange) {
        metaMapper.insertExchange(exchange);
    }

    public void deleteExchange(String exchangeName) {
        metaMapper.deleteExchange(exchangeName);
    }

    public List<Exchange> selectAllExchanges() {
        return metaMapper.selectAllExchanges();
    }

    public void insertQueue(MSGQueue queue) {
        metaMapper.insertQueue(queue);
    }

    public void deleteQueue(String queueName) {
        metaMapper.deleteQueue(queueName);
    }

    public List<MSGQueue> selectAllQueues() {
        return metaMapper.selectAllQueues();
    }

    public void insertBinding(Binding binding) {
        metaMapper.insertBinding(binding);
    }

    public void deleteBinding(Binding binding) {
        metaMapper.deleteBinding(binding);
    }

    public List<Binding> selectAllBindings() {
        return metaMapper.selectAllBindings();
    }
}
